package com.harmontronics.hanma.paas.paas.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import com.harmontronics.hanma.paas.paas.annotation.Log;
import com.harmontronics.hanma.paas.paas.common.PageBase;
import com.harmontronics.hanma.paas.paas.common.ResponseEntity;
import com.harmontronics.hanma.paas.paas.domain.app.entity.HmProcess;
import com.harmontronics.hanma.paas.paas.dto.input.IdInput;
import com.harmontronics.hanma.paas.paas.dto.input.ProcessMoveInput;
import com.harmontronics.hanma.paas.paas.dto.input.process.*;
import com.harmontronics.hanma.paas.paas.dto.output.HmProcessResult;
import com.harmontronics.hanma.paas.paas.service.HmProcessAppService;
import com.harmontronics.hanma.paas.paas.utils.ParamUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;


/**
 * ${comments}
 *
 * @author paas-generator
 * @email paas-generator@harmontronics.com
 * @date 2021-04-02 10:54:59
 */
@RestController
@RequestMapping("/api/hm-process")
@Api(tags = {"流程"})
public class HmProcessController {
    @Autowired
    private HmProcessAppService hmProcessappService;

    /**
     * 列表
     */
    @GetMapping("/list")
    @ApiOperation("列表")
    public ResponseEntity<List<HmProcessResult>> list(@RequestParam(required = false) Boolean activated) {
        List<HmProcess> hmProcessList = hmProcessappService.getList(activated);

        return ResponseEntity.ok(Convert.toList(HmProcessResult.class, hmProcessList));
    }


    @GetMapping("history")
    @ApiOperation("历史版本")
    public ResponseEntity<List<HmProcessResult>> getHistory(@ApiParam(value = "流程KEY") @RequestParam String key) {
        List<HmProcess> hmProcessList = hmProcessappService.getHistoryList(key);

        return ResponseEntity.ok(Convert.toList(HmProcessResult.class, hmProcessList));
    }

    /**
     * 分页列表
     */
    @GetMapping("/page/list")
    @ApiOperation("分页列表")
    public ResponseEntity<PageBase<HmProcessResult>> list(
            @ApiParam(value = "文件夹ID") @RequestParam(required = false) Long folderId,
            @ApiParam(value = "查找关键字") @RequestParam(required = false) String searchKey,
            @ApiParam(value = "页码", defaultValue = "1") @RequestParam(required = false, defaultValue = "1") int pageNo,
            @ApiParam(value = "每页数据条数", defaultValue = "20") @RequestParam(required = false, defaultValue = "20") int pageSize) {

        PageBase<HmProcess> hmProcessPageBase = hmProcessappService.getList(folderId, searchKey, pageNo, pageSize);
        PageBase<HmProcessResult> resultPageBase = PageBase.copy(hmProcessPageBase, HmProcessResult.class);
        return ResponseEntity.ok(resultPageBase);
    }


    /**
     * 信息
     */
    @GetMapping("/info")
    @ApiOperation("详情")
    public ResponseEntity<HmProcessResult> info(@RequestParam("id") Long id, @RequestParam(value = "includeXml", required = false) Boolean includeXml) {
        HmProcess hmProcess = hmProcessappService.findById(id, includeXml);
        HmProcessResult result = BeanUtil.toBean(hmProcess, HmProcessResult.class);
        return ResponseEntity.ok(result);
    }

    /**
     * 保存
     */
    @PostMapping()
    @ApiOperation("保存")
    @Log("创建了流程")
    public ResponseEntity<String> save(@Valid @RequestBody HmProcessCreateInput hmProcessInput, BindingResult bindingResult) {
        ParamUtils.valid(bindingResult);
        HmProcess hmProcess = BeanUtil.toBean(hmProcessInput, HmProcess.class);
        hmProcessappService.save(hmProcess);

        return ResponseEntity.ok();
    }


    @GetMapping("start-form-key")
    @ApiOperation("获取开始节点表单ID")
    public ResponseEntity<Long> getStartFormId(@RequestParam Long processId) {
        Long formId = hmProcessappService.getStartFormId(processId);
        return ResponseEntity.ok(formId);
    }

    @GetMapping("usable")
    @ApiOperation("根据模型ID获取可用的流程")
    public ResponseEntity<List<HmProcessResult>> getUsableProcess(@RequestParam Long modelId) {
        List<HmProcess> list = hmProcessappService.getUsableProcess(modelId);

        List<HmProcessResult> results = Convert.toList(HmProcessResult.class, list);
        return ResponseEntity.ok(results);
    }


    /**
     * 创建新版本
     */
    @PostMapping("make-version")
    @ApiOperation("创建新版本")
    @Log("创建了流程新版本")
    public ResponseEntity<String> save(@Valid @RequestBody HmProcessVersionInput versionInput) {

        hmProcessappService.makeVersion(versionInput.getSourceId(),versionInput.getSourceKey(), versionInput.getDescription());

        return ResponseEntity.ok();
    }


    @PostMapping("copy")
    @ApiOperation("拷贝流程")
    @Log("复制了流程")
    public ResponseEntity<String> copy(@Valid @RequestBody HmProcessCopyInput copyInput) {
        hmProcessappService.copy(copyInput.getSourceId(), copyInput.getName(), copyInput.getKey(), copyInput.getDescription());
        return ResponseEntity.ok();
    }

    /**
     * 激活版本
     */
    @PutMapping("activate")
    @ApiOperation("激活版本")
    @Log("激活了流程版本")
    public ResponseEntity<String> activate(@Valid @RequestBody IdInput idInput) {
        hmProcessappService.activateVersion(idInput.getId());

        return ResponseEntity.ok();
    }

    /**
     * 激活版本
     */
    @PutMapping("publish")
    @ApiOperation("发布版本")
    @Log("发布了流程版本")
    public ResponseEntity<String> publish(@Valid @RequestBody IdInput idInput) {
        hmProcessappService.publish(idInput.getId());

        return ResponseEntity.ok();
    }

    /**
     * 保存部署流程设计
     *
     * @param hmProcessJsonInput
     * @param bindingResult
     * @return
     */
    @PutMapping("deploy")
    @ApiOperation("保存部署流程设计")
    @Log("设计部署了流程")
    public ResponseEntity<String> design(@Valid @RequestBody HmProcessJsonInput hmProcessJsonInput, BindingResult bindingResult) {
        ParamUtils.valid(bindingResult);
        hmProcessappService.deploy(hmProcessJsonInput.getId(), hmProcessJsonInput.decodeJson());
        return ResponseEntity.ok();
    }

    /**
     * 修改
     */
    @PutMapping
    @ApiOperation("修改")
    @Log("修改了流程")
    public ResponseEntity<String> update(@Valid @RequestBody HmProcessUpdateInput hmProcessInput, BindingResult bindingResult) {
        ParamUtils.valid(bindingResult);
        HmProcess hmProcess = BeanUtil.toBean(hmProcessInput, HmProcess.class);
        hmProcessappService.update(hmProcess);
        return ResponseEntity.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping()
    @ApiOperation("删除")
    @Log("删除了流程")
    public ResponseEntity<String> delete(@RequestBody Long[] ids) {
        hmProcessappService.remove(Arrays.asList(ids));

        return ResponseEntity.ok();
    }


    /**
     * 移动流程
     */
    @PutMapping("move")
    @ApiOperation("移动流程")
    @Log("移动了流程")
    public ResponseEntity<String> move(@RequestBody ProcessMoveInput moveInput) {
        hmProcessappService.move(moveInput.getId(), moveInput.getFolderId());
        return ResponseEntity.ok();
    }

    /**
     * 检验字段是否重复
     *
     * @param field
     * @param value
     * @param id
     * @return
     */
    @GetMapping("valid-field")
    @ApiOperation("字段重复检查")
    public ResponseEntity<String> validField(@RequestParam String field, @RequestParam String value, @RequestParam(required = false) Long id) {
        hmProcessappService.validField(id, field, value);
        return ResponseEntity.ok();
    }

}
